/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ga;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author mbsn
 */
public class Individuum {

    public Individuum(Graph graph) {
        this.graph = graph;
      
    }
    
    public Individuum (List<Path> paths){
        this.paths = paths;
    }
            
    public Individuum(){}
              
    private List<Path> paths = new ArrayList<Path>();
    private Timetable timetable = new Timetable();
    private Graph graph;
    private BigDecimal averageTime = new BigDecimal(0);

    public void addPath(Path p){
        getPaths().add(p);
    }

    public List<Path> getPaths() {
        return paths;
    }

    public void setPaths(ArrayList<Path> paths) {
        this.paths = paths;
    }

    private void generateIndividuum(Graph g, int carsNumber){
       
    }
        
    @Override
    public String toString(){
        String res = "individuum:\n";
        //BigDecimal b = countAverageTime();  
        for(int i = 0; i < getPaths().size(); i++){
         
          res = res + getPaths().get(i).toString() + "\n";
        }
        res = res + averageTime.toString() + "\n";
        return res;
    }
    
    public BigDecimal countAverageTime(){
     
        System.out.println(toString());
        timetable = new Timetable(graph);// надо вынести инициализацию отдельно будет
        timetable.fillTimetableForIndividuum(this);
        
        System.out.println("timetable: \n" + timetable.printTimetable());
        timetable.sortTimetable();
        System.out.println("sorted timetable: \n" + timetable.printTimetable());
        timetable.mergeTimetable();
        System.out.println("merged timetable: \n" + timetable.printTimetable());
      
        BigDecimal averageForIndividuum = new BigDecimal(0);
        int maxElement = timetable.getMaxEndElement();
        
        System.out.println("maxElement: " + maxElement);
        
        for(int i = 0; i < timetable.getTimetable().size(); i++){
            //System.out.println("i: " + i);
            BigDecimal averageForPath = new BigDecimal(0);
                int intervals = timetable.countOutsideIntervals(i);
                int time = timetable.countTimeOutsideIntervals(i);
                if(intervals > 0 && time > 0){
                    BigDecimal a1 = new BigDecimal(time);
                    BigDecimal a2 = new BigDecimal(intervals);
                    averageForPath = a1.divide(a2, 3);
                }
//                else if(intervals == -1 && time == maxElement){
//                    averageForPath = new BigDecimal(maxElement);
//                }
                System.out.println("intervals: " + intervals + " time: " + time + " av: " + averageForPath);
                averageForIndividuum = averageForIndividuum.add(averageForPath);
            
        }
        averageForIndividuum = averageForIndividuum.divide(new BigDecimal(timetable.getTimetable().size()), 3);
        averageTime = averageForIndividuum;
        return averageForIndividuum;
    }

    public BigDecimal getRes(){
        return getAverageTime();
    }

    /**
     * @return the averageTime
     */
    public BigDecimal getAverageTime() {
        return averageTime;
    }
}
